<!DOCTYPE HTML>
<html>

<head>
    <title>three.proton - helloworld</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <style type="text/css">
    body {
        font-family: Monospace;
        background-color: #fff;
        margin: 0;
        padding: 0;
        overflow: hidden;
    }
    </style>
</head>

<body>
    <div id="container"></div>
    <script src="../lib/stats.min.js"></script>
    <script src="../lib/three.min.js"></script>
    <script src="../build/three.proton.min.js"></script>
    <script src="./js/lib/TrackballControls.js"></script>
    <script>
    var proton, emitter;
    var camera, scene, renderer, stats, clock, spring, controls;

    init();

    function init() {
        initScene();
        initLights();
        initProton();
        initControls();
        addStats();
        animate();
    }

    function initScene() {
        camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000);
        camera.position.z = 500;
        scene = new THREE.Scene();
        scene.fog = new THREE.Fog(0xffffff, 1, 10000);

        clock = new THREE.Clock();

        renderer = new THREE.WebGLRenderer();
        renderer.setPixelRatio(window.devicePixelRatio);
        renderer.setSize(window.innerWidth, window.innerHeight);

        document.body.appendChild(renderer.domElement);
        window.addEventListener('resize', onWindowResize, false);
    }

    function initLights() {
        var ambientLight = new THREE.AmbientLight(0x101010);
        scene.add(ambientLight);
        var pointLight = new THREE.PointLight(0xffffff, 2, 1000, 1);
        pointLight.position.set(0, 200, 200);
        scene.add(pointLight);
    }

    function initControls() {
        controls = new THREE.TrackballControls(camera);
        controls.rotateSpeed = 1.0;
        controls.zoomSpeed = 1.2;
        controls.panSpeed = 0.8;
        controls.noZoom = false;
        controls.noPan = false;
        controls.staticMoving = true;
        controls.dynamicDampingFactor = 0.3;
        //controls.addEventListener('change', render);
    }

    function addStats() {
        stats = new Stats();
        stats.showPanel(0);
        stats.dom.style.position = 'absolute';
        stats.dom.style.left = '0px';
        stats.dom.style.top = '0px';
        container.appendChild(stats.dom);
    }

    function initProton() {
        proton = new Proton();
        proton.addEmitter(createEmitter());
        proton.addRender(new Proton.MeshRender(scene));
    }

    function createBox() {
        var geometry = new THREE.BoxGeometry(20, 20, 20);
        var material = new THREE.MeshLambertMaterial({
            color: "#00ffcc"
        });

        return new THREE.Mesh(geometry, material);
    }


    function createEmitter() {
        emitter = new Proton.Emitter();
        emitter.rate = new Proton.Rate(new Proton.Span(6, 12), new Proton.Span(.2, .5));
        emitter.addInitialize(new Proton.Mass(1));
        emitter.addInitialize(new Proton.Radius(100));
        emitter.addInitialize(new Proton.Life(2, 4));
        emitter.addInitialize(new Proton.Body(createBox()));

        emitter.addInitialize(new Proton.Velocity(300, new Proton.Vector3D(0, 1, 0), 50));

        // //emitter.addBehaviour(new Proton.RandomDrift(30, 30, 30, .05));
        emitter.addBehaviour(new Proton.Rotate("random", "random"));
        emitter.addBehaviour(new Proton.Scale(1, .1));
        // emitter.addBehaviour(new Proton.Alpha(1, 0, Infinity, Proton.easeInQuart));
        var zone2 = new Proton.BoxZone(500);
        //emitter.addBehaviour(new Proton.CrossZone(zone2, "bound"));
        spring = new Proton.Spring(100, 100, 100);
        emitter.addBehaviour(spring);
        emitter.addBehaviour(new Proton.Color('random', 'random', Infinity, Proton.easeOutQuart));

        emitter.p.x = 0;
        emitter.p.y = 0;
        emitter.emit();

        ///Proton.Debug.drawZone(proton,scene,zone2);
        Proton.Debug.drawEmitter(proton, scene, emitter);
        return emitter;
    }

    function animate() {
        stats.begin();
        requestAnimationFrame(animate);
        render();
        stats.end();
    }

    var tha = 0,
        R = 200;

    function render() {
        proton.update(clock.getDelta());
        renderer.render(scene, camera);

        tha += .005;
        proton.emitters[0].p.x = Math.cos(tha) * R;
        proton.emitters[0].p.y = Math.sin(tha) * R;
        proton.emitters[0].rotation.x += 0.01;
        var x = Math.cos(tha) * 100;
        var y = Math.sin(tha) * 100;
        spring.reset(x, y, 100);
        controls.update();

        Proton.Debug.renderInfo(proton, 3);
    }

    function onWindowResize() {

    }
    </script>
</body>

</html>
